<template>
  <div>
    <!-- <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>岛民记录</el-breadcrumb-item>
      <el-breadcrumb-item>岛民邮箱</el-breadcrumb-item>
    </el-breadcrumb> -->
    <el-card shadow="never">
      <el-table :data="emailList" border width="50%">
        <el-table-column type="index" width="50" label="#">
        </el-table-column>
        <el-table-column label="用户名" width="100" prop="user"></el-table-column>
        <el-table-column label="头像" width="100">
          <template v-slot:default="scope">
            <el-image :src="scope.row.avatar" style="width:60px;height:60px;border-radius: 50%;"></el-image>
          </template>
        </el-table-column>
        <el-table-column label="标题" :show-overflow-tooltip="true" prop="title" width="200"></el-table-column>
        <el-table-column label="内容" :show-overflow-tooltip="true" prop="content"></el-table-column>
        <el-table-column label="日期" width="180" prop="create_time"></el-table-column>
        <el-table-column label="操作" width="100">
          <template v-slot:default="scope">
            <el-button type="danger" @click="deleteCommentTap(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination background layout="prev, pager, next" :total='emailTotal' :page-size="queryInfo.perpage" @current-change="handleCurrentChange" :current-page="queryInfo.pagenum">
      </el-pagination>
    </el-card>
  </div>
</template>
<script>
import { getEmailListApi, deleteEmailApi } from '../../request/apis'
export default {
  data () {
    return {
      queryInfo: {
        pagenum: 1,
        perpage: 8
      },
      emailList: [],
      emailTotal: 0
    }
  },
  created () {
    this.getEmailList()
  },
  methods: {
    async getEmailList () {
      const { data, meta } = await getEmailListApi({ params: this.queryInfo })
      if (meta.status === 200) {
        this.emailList = data.emails
      } else {
        this.$message.error('获取分享列表失败')
      }
    },
    handleCurrentChange (newpage) {
      this.queryInfo.pagenum = newpage
      this.getEmailList()
    },
    async deleteCommentTap (id) {
      const confirmResult = await this.$confirm('删除该用户信息', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => {
        return err
      })
      if (confirmResult !== 'confirm') {
        return this.$message.info('取消')
      }
      const { data, meta } = await deleteEmailApi({ params: { id } })
      if (meta.status === 200) {
        const pagenum = this.queryInfo.pagenum
        this.$message.success('删除成功！')
        if ((data.total % this.queryInfo.perpage) === 0) {
          pagenum > 1 ? this.queryInfo.pagenum -= 1 : this.queryInfo.pagenum = 1
        }
        this.getEmailList()
      }
    }
  }
}
</script>

<style>
</style>
